/*
 * @Author: song
 * @Description: Inspector面板
 * @Date: 2024年05月03日 星期五 22:05:48
 * @Modify:
 */

using SCore.Editor.WindowDraw;
using UnityEditor;
using UnityEngine;

namespace Function.SkillEditor.Editor
{
    public enum IInspectorType
    {
        Up,
        Down
    }

    public interface IInspector
    {
        void OnInspector(Rect rect);
    }

    public class SkillInspectorSubWindow : IESubWindow
    {
        private IInspector _upInspector;
        private IInspector _downInspector;
        private Rect _upRect, _downRect;

        public SkillInspectorSubWindow(EditorWindow window) : base(window)
        {
        }

        public override void OnEnable()
        {
            SkillEditorWindow.EventManager.AddListener<IInspectorType, IInspector>(
                SkillEditorEventType.InspectorTargetChange,
                OnInspectorTargetChange);
            base.OnEnable();
        }

        public override void OnDisable()
        {
            SkillEditorWindow.EventManager.RemoveListener<IInspectorType, IInspector>(
                SkillEditorEventType.InspectorTargetChange,
                OnInspectorTargetChange);
            base.OnDisable();
        }

        public override void OnGUI()
        {
            using (new GUILayout.VerticalScope("box", GUILayout.Height(200)))
            {
                DrawTitle("编辑Line");
                _upInspector?.OnInspector(_upRect);
            }

            var upRect = GUILayoutUtility.GetLastRect();
            if (upRect != Rect.zero && upRect != new Rect(0, 0, 1, 1))
            {
                _upRect = new Rect(upRect.x, upRect.y + 20, upRect.width, upRect.height);
            }

            using (new GUILayout.VerticalScope("box", GUILayout.ExpandHeight(true)))
            {
                DrawTitle("编辑Track");
                _downInspector?.OnInspector(_downRect);
            }

            var downRect = GUILayoutUtility.GetLastRect();
            if (downRect != Rect.zero && downRect != new Rect(0, 0, 1, 1))
            {
                _downRect = new Rect(downRect.x, downRect.y + 20, downRect.width, downRect.height);
            }
        }

        private void OnInspectorTargetChange(IInspectorType inspectorType, IInspector inspector)
        {
            if (inspectorType == IInspectorType.Up)
            {
                _upInspector = inspector;
            }
            else if (inspectorType == IInspectorType.Down)
            {
                _downInspector = inspector;
            }
        }

        protected override bool MouseDown(Event evt)
        {
            if (evt.button == 0)
            {
                GUI.FocusControl(null);
                GetWindow<SkillEditorWindow>().Repaint();
                evt.Use();
                return true;
            }

            return false;
        }
    }
}